#
# This file is part of uscp, an open-source cross-platform package tool
# 2008  Rafael Sachetto
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Contact e-mail: Rafael Sachetto <rsachetto@gmail.com>
# Program URL: http://code.google.com/p/uspc/

#USPC library imports
from optionsdialogui import Ui_OptionsDialog
from messages import *
from defines import ARCH_INDEX, CUR_RELEASE_INDEX, RELEASES, DISTS, ARCHS

#PyQt4 imports
from PyQt4.QtGui import QDialog, QCompleter, QDirModel, QFileDialog
from PyQt4.QtCore import SIGNAL, QSettings, QVariant, QDir

class OptionsDialog(QDialog, Ui_OptionsDialog):
    
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)        
      
        self.connect(self.distComboBox,
                     SIGNAL("currentIndexChanged (const QString &)"),
                     self.changeReleases)


        self.distComboBox.insertItems(0, DISTS)

        self.connect(self.okButton, SIGNAL("clicked()"), self.writeSettings);
        self.connect(self.browseOutputButton, SIGNAL("clicked()"), self.browseOutputFolder)
        

        self.completer = QCompleter(self)
        dirModel = QDirModel(self.completer)
        self.completer.setModel(dirModel)
        self.outputLineEdit.setCompleter(self.completer)
        self.readSettings();

    #----------------------------------------------------------------------
    def writeSettings(self):
        
        """Writes all the setting to a file (Linux/Mac OS) or
           to the register (Windows)
        """            
        
        settings = QSettings("USPC")
        
        settings.beginGroup("USPC")
        settings.setValue("defaultOutputFolder", QVariant(self.outputLineEdit.text()))
        settings.setValue("defaultArchiteture", QVariant(self.archComboBox.currentIndex()))
        settings.setValue("defaultDist", QVariant(self.distComboBox.currentIndex()))
        settings.setValue("defaultVersion", QVariant(self.versionComboBox.currentIndex()))
        settings.setValue("usePackageCache", QVariant(self.cacheCheckBox.isChecked()))
        settings.setValue("listDefaultPackages", QVariant(self.listDefaultCheckBox.isChecked()))
        settings.setValue("nDownloadThreads", QVariant(self.numThreadsSpinBox.value()))
        settings.endGroup()
        
        self.emit(SIGNAL("ok()"))
        self.close()
        
    #----------------------------------------------------------------------
    def readSettings(self):
        """Reads all the settings and populates the options dialog widgets"""
            
        defaultOutputDir = QVariant(QDir.homePath())
        
        settings = QSettings("USPC")
        
        settings.beginGroup("USPC")
        self.outputLineEdit.setText(settings.value("defaultOutputFolder", defaultOutputDir).toString())
        self.archComboBox.setCurrentIndex(settings.value("defaultArchiteture", QVariant(ARCH_INDEX)).toInt()[0])
        self.distComboBox.setCurrentIndex(settings.value("defaultDist", QVariant(0)).toInt()[0])
        self.versionComboBox.setCurrentIndex(settings.value("defaultVersion", QVariant(0)).toInt()[0])
        self.cacheCheckBox.setChecked(settings.value("usePackageCache", QVariant(False)).toBool())
        self.listDefaultCheckBox.setChecked(settings.value("listDefaultPackages", QVariant(False)).toBool())
        self.numThreadsSpinBox.setValue(settings.value("nDownloadThreads", QVariant(4)).toInt()[0])
        settings.endGroup()
        
    #----------------------------------------------------------------------
    def browseOutputFolder(self):
        """
        Called when the browse destination button is cliked.
        Opens a browse dialog to set the output folder
        """

        initialName = self.outputLineEdit.text()

        if initialName.isEmpty():
            initialName = QDir.homePath()

        fileName = QFileDialog.getExistingDirectory(
                self,
                CHOOSE_DIR,
                initialName,
                QFileDialog.ShowDirsOnly)

        fileName = QDir.convertSeparators(fileName)

        if not fileName.isEmpty():
            self.outputLineEdit.setText(fileName)
        
    #----------------------------------------------------------------------
    def changeReleases(self, distribution):

        releases_list = RELEASES[str(distribution)]

        if distribution == "Ubuntu":
            self.versionComboBox.clear()
            self.versionComboBox.insertItems(0, releases_list)
            
        elif distribution == "Debian":
            self.versionComboBox.clear()
            self.versionComboBox.insertItems(0, releases_list)

